package com.cnfilm.utils;

public class TargetUtils {
	
	private static final String[] NO_FILTER = {".css",".js",".png",".gif",".jpg",".ico"};
	
	/**
	 * 判断URL是否需要过滤
	 * @param starget
	 * @return
	 */
	private static boolean filter(String starget){
		boolean filter = true;
		for(String str:NO_FILTER){
			if(starget.endsWith(str)){
				filter = false;
				break;
			}
		}
		return filter;
	}
	
	/**
	 * URL重写规则处理
	 * @param starget
	 * @return
	 */
	public static String getTarget(String starget){
		if(filter(starget)){
			/**网站首页和后台不过滤**/
			if(starget.equals("/") || starget.equals("") || starget.contains("sysadmin")){
				return starget;
			}else{
				  String[] arr =  null;
				  /**去掉开始的斜杠**/
				  if(starget.startsWith("/")){
					  arr = starget.substring(1, starget.length()).split("/");
				  }else{
					  arr = starget.split("/");
				  }
				  int len = arr.length;
				  if(len==1){
					  String url = arr[0];
					  String[] params = url.split("_");
					  int paramslen = params.length;
					  if(paramslen==1){
						  if(url.equals("search")){
							 starget = "/film/search";
						  }else if(url.equals("rss.xml")){
							 starget = "/film/rss";
						  }else if(url.equals("news")){
							 starget = "/film/news";
						  }else if(url.equals("gbook")){
							 starget = "/film/gbook";
						  }else if(url.equals("bdhd")){
							 starget = "/film/bdhd";
						  }else if(url.equals("sitemap")){
							 starget = "/film/sitemap";
						  }else if(url.equals("tags")){
							 starget = "/film/tags";
						  }else{
							 starget = "/film/list/?url=" + url;
						  }
					  }else if(paramslen==2){
						  url = params[0];
						  String page = params[1];
						  if(url.equals("news")){
							  int pnum = 1;
							  try{pnum = Integer.parseInt(page);}catch(Exception e){ pnum = 1; }
							  starget = "/film/news/?page=" + pnum;
						  }else  if(url.equals("bdhd")){
							  int pnum = 1;
							  try{pnum = Integer.parseInt(page);}catch(Exception e){ pnum = 1; }
							  starget = "/film/bdhd/?page=" + pnum;
						  }else if(url.equals("gbook")){
							  if(page.equals("add")){
								  starget = "/film/addgbook";
							  }else{
								  int pnum = 1;
								  try{pnum = Integer.parseInt(page);}catch(Exception e){ pnum = 1; }
								  starget = "/film/gbook/?page=" + pnum;
							  }
						  }else if(url.equals("area")){
							  starget = "/film/area/?area=" + page;
						  }else{
							  int pnum = 1;
							  try{pnum = Integer.parseInt(page);}catch(Exception e){ pnum = 1; }
							  starget = "/film/list/?url=" + url + "&page=" + pnum;
						  }
					  }else if(paramslen==3){
						  url = params[0];
						  String area = params[1];
						  String page = params[2];
						  if(url.equals("area")){
							  int pnum = 1;
							  try{pnum = Integer.parseInt(page);}catch(Exception e){ pnum = 1; }
							  starget = "/film/area/?area="+area+"&page=" + pnum;
						  }
					  }
				  }else if(len==2){
					  String curl = arr[0];
					  String furl = arr[1];
					  if(curl.equals("tag")){
						  starget = "/film/tagfilm/?tname=" + furl;
					  }else{
						  if(furl.endsWith("_")){
							  furl = furl+"0";
						  }
						  String[] params = furl.split("_");
						  int paramslen = params.length;
						  if(paramslen==1){
							  starget = "/film/getfilm/?curl=" + curl + "&furl=" + furl;
						  }else if(paramslen==2){
							  furl = params[0];
							  String vid = params[1];
							  if(vid.equals("score")){
								  starget = "/film/score/?curl=" + curl + "&furl=" + furl;
							  }else{
								  if(vid.equals("0")){
									  starget = "/film/getfilm/?curl=" + curl + "&furl=" + furl;
								  }else{
									  starget = "/film/getvideo/?curl=" + curl + "&furl=" + furl + "&vid=" + vid;
								  }
							  }
						  }
						  
					  }
				  }
				  return starget;
			}
		}else{
			return starget;
		}
	}

}
